<template>
  <div class="scan">
    <div class="bcidHeader">
      <i class="iconfont icon-back" @click="back"></i>
      <span @click="setFlash">二维码扫描</span>
      <i></i>
    </div>
    <div id="bcid"></div>
    <footer>
      <!-- <button @click="startRecognize">1.创建控件</button>
      <button @click="startScan">2.开始扫描</button>
      <button @click="cancelScan">3.结束扫描</button>
      <button @click="closeScan">4.关闭控件</button>-->
    </footer>
  </div>
</template>

<script type='text/ecmascript-6'>
let scan = null;
var data = null;
export default {
  data() {
    return {
      codeUrl: "",
      data: ""
    };
  },
  mounted() {
    this.startRecognize();
  },
  watch:{
    $route(to,form){
      console.log(to)
    }
  },
  methods: {
    //创建扫描控件
    startRecognize() {
      let that = this;
      if (!window.plus) return;
      scan = new plus.barcode.Barcode("bcid");
      scan.onmarked = onmarked;

      function onmarked(type, result, file) {
        switch (type) {
          case plus.barcode.QR:
            type = "QR";
            break;
          case plus.barcode.EAN13:
            type = "EAN13";
            break;
          case plus.barcode.EAN8:
            type = "EAN8";
            break;
          default:
            type = "其它" + type;
            break;
        }
        result = result.replace(/\n/g, "");

        this.data = result;
        data = result;
        that.codeUrl = result;
        alert(result);
        // var one = result.substr(0, 1);
        // if (one == "0") {
          that.closeScan();
        // } else {
        //   alert("从新扫描");
        // }
      }
    },
    //开始扫描
    startScan() {
      if (!window.plus) return;
      scan.start();
    },
    //关闭扫描
    cancelScan() {
      if (!window.plus) return;
      scan.cancel();
    },
    //关闭条码识别控件
    closeScan() {
      if (!window.plus) return;
      scan.close();
      this.$store.state.ScanData = data
      this.back()
  
    },
    setFlash() {
      scan.setFlash();
    },
    back() {
      this.$router.go(-1);
      if (!window.plus) return;
      scan.close();
    }
  }
};
</script>
<style lang="less">
.scan {
  height: 100%;
  #bcid {
    width: 100%;
    position: absolute;
    height: 92.5%;
    left: 0;
    right: 0;
    bottom: 0rem;
    text-align: center;
    color: #fff;
    background: #ccc;
    z-index: -9999;
  }
  footer {
    position: absolute;
    left: 0;
    bottom: 1rem;
    height: 2rem;
    line-height: 2rem;
    z-index: 2;
  }
  .bcidHeader {
    width: 100%;
    height: 1rem;
    background: rgba(252, 1, 1, 0.5);
    line-height: 1rem;
    text-align: center;
    position: absolute;
    z-index: 99999;
    top: 0;
    left: 0;
  }
  .bcidHeader i {
    position: absolute;
    left: 0.2rem;
    display: inline-block;
    color: #fff;
  }
  .bcidHeader span {
    display: inline-block;
    margin: 0 auto;
    color: #fff;
  }
}
</style>
